package com.mfin.bom.factory;

import com.mfin.bom.adapter.CashbookAdapter;
import com.mfin.bom.adapter.ChitMasterAdapter;
import com.mfin.bom.adapter.CustomerAdapter;
import com.mfin.bom.adapter.FinanceAdapter;
import com.mfin.bom.adapter.PartnerAdapter;
import com.mfin.bom.adapter.UserAdapter;
import com.mfin.bom.factory.interfaces.IFinanceInformationFactory;
import com.mfin.bom.interfaces.ICashbook;
import com.mfin.bom.interfaces.IChitMaster;
import com.mfin.bom.interfaces.ICustomer;
import com.mfin.bom.interfaces.IFinance;
import com.mfin.bom.interfaces.IPartner;
import com.mfin.bom.interfaces.IUser;
import com.mfin.bom.objects.Cashbook;
import com.mfin.bom.objects.ChitMaster;
import com.mfin.bom.objects.Customer;
import com.mfin.bom.objects.Finance;
import com.mfin.bom.objects.Partner;
import com.mfin.bom.objects.User;
import com.mfin.vo.ObjectKey;
import com.mfin.vo.interfaces.IIdentifiableObject;

public class FinanceInformationFactory extends BaseFactory implements
		IFinanceInformationFactory {

	@Override
	public IFinance getFinanceInformation(IIdentifiableObject identifiableObject) {

		IFinance ba = null;
		ObjectKey[] keys = identifiableObject.getKeys();

		try {
			Finance fb = (Finance) getPersistenceGovernor().get(Finance.class,
					keys[0].getId());

			if (fb != null) {
				ba = new FinanceAdapter(fb);
			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return ba;
	}

	@Override
	public ICustomer createCustomer(IIdentifiableObject identifiableObject) {
		ICustomer ci = null;

		try {
			Customer c = new Customer();

			if (c != null) {
				getPersistenceGovernor().insert(c);
				ci = new CustomerAdapter(c, identifiableObject.getKeys());
			}
		} catch (Exception ex) {
			// throw bom runtime exception
			ex.printStackTrace();
		}

		return ci;
	}

	@Override
	public IFinance createFinance() {

		IFinance mi = null;

		try {
			Finance pf = new Finance();

			if (pf != null) {
				getPersistenceGovernor().insert(pf);

				mi = new FinanceAdapter(pf);
				ObjectKey[] keys = new ObjectKey[1];
				keys[0] = new ObjectKey(new Long(pf.getId()));
				mi.setKeys(keys);
			}
		} catch (Exception ex) {
			// throw bom runtime exception
			ex.printStackTrace();
		}

		return mi;
	}

	@Override
	public IPartner createPartner(IIdentifiableObject identifiableObject) {
		IPartner pa = null;
		ObjectKey[] keys =  identifiableObject.getKeys();
		try {
			Partner p = new Partner();

			if (p != null) {
				getPersistenceGovernor().insert(p);
				pa = new PartnerAdapter(p, keys);
			}
		} catch (Exception ex) {
			// throw bom runtime exception
			ex.printStackTrace();
		}

		return pa;
	}

	@Override
	public IUser createUser(IIdentifiableObject identifiableObject) {
		IUser us = null;

		try {
			User u = new User();

			if (u != null) {
				getPersistenceGovernor().insert(u);
				us = new UserAdapter(u, identifiableObject.getKeys());
			}
		} catch (Exception ex) {
			// throw bom runtime exception
			ex.printStackTrace();
		}

		return us;
	}

	@Override
	public ICashbook createCashBookEntry(IIdentifiableObject identifiableObject) {
		ICashbook cb = null;
		ObjectKey[] keys = identifiableObject.getKeys();
		try {
			Cashbook c = new Cashbook();

			if (c != null) {
				getPersistenceGovernor().insert(c);
				cb = new CashbookAdapter(c, keys);
			}
		} catch (Exception ex) {
			// throw bom runtime exception
			ex.printStackTrace();
		}

		return cb;
	}

	@Override
	public IChitMaster createChitMaster(IIdentifiableObject identifiableObject) {
		IChitMaster cm = null;
		ObjectKey[] keys = identifiableObject.getKeys();
		try {
			ChitMaster c = new ChitMaster();

			if (c != null) {
				getPersistenceGovernor().insert(c);
				cm = new ChitMasterAdapter(c, keys);
			}
		} catch (Exception ex) {
			// throw bom runtime exception
			ex.printStackTrace();
		}

		return cm;
	}
}
